package jia.game;

import jia.core.GameStateManager;
import jia.core.characters.GameCharacter;
import org.apache.log4j.Logger;

import javax.swing.*;
import javax.swing.table.JTableHeader;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

/**
 * Custom table component that lists the player characters and NPC's currently
 * in the party. Double clicking on a name in the list should open the
 * character's information screen.
 *
 * TODO:  Add logic to update the table whenever something happens to one of the GameCharacters
 *
 * @author Erik Stell
 * @version 1.0
 * @see JTable
 * @see MouseListener
 * @since 01/18/2006
 */
public class PlayerCharacterList
    extends JTable
    implements MouseListener {
    private TableHandler tableHandler = new TableHandler();
    private GameStateManager mgr;
    private static final Logger log = Logger.getLogger(PlayerCharacterList.class);
    
    /**
     * Default Constructor
     */
    public PlayerCharacterList(GameStateManager mgr) {
        super();
        
        this.mgr = mgr;
        initTableFormatting();
        this.setModel(tableHandler);
        
        GameCharacter[] characters = mgr.getGameCharacters();
        tableHandler.addRowToTable(characters[0].getCharacterName(), characters[0].getPlayerClass().getPlayerClassName(), Integer.toString(characters[0].getCharacterHP()));
        tableHandler.addRowToTable(characters[1].getCharacterName(), characters[1].getPlayerClass().getPlayerClassName(), Integer.toString(characters[1].getCharacterHP()));
        tableHandler.addRowToTable(characters[2].getCharacterName(), characters[2].getPlayerClass().getPlayerClassName(), Integer.toString(characters[2].getCharacterHP()));
        tableHandler.addRowToTable(characters[3].getCharacterName(), characters[3].getPlayerClass().getPlayerClassName(), Integer.toString(characters[3].getCharacterHP()));
        tableHandler.addRowToTable(characters[4].getCharacterName(), characters[4].getPlayerClass().getPlayerClassName(), Integer.toString(characters[4].getCharacterHP()));
        tableHandler.addRowToTable(characters[5].getCharacterName(), characters[5].getPlayerClass().getPlayerClassName(), Integer.toString(characters[5].getCharacterHP()));
    }
    
    public void updateTable(){
        GameCharacter[] characters = mgr.getGameCharacters();
        tableHandler.updateTableRow(0, characters[0]);
        tableHandler.updateTableRow(1, characters[1]);
        tableHandler.updateTableRow(2, characters[2]);
        tableHandler.updateTableRow(3, characters[3]);
        tableHandler.updateTableRow(4, characters[4]);
        tableHandler.updateTableRow(5, characters[5]);
    }
    
    public void updateSingleRow(GameCharacter character, int index){
        tableHandler.updateTableRow(index, character);
    }
    
    private void initTableFormatting() {
        this.setSize(520, 150);
        this.setVisible(true);
        this.setBackground(ComponentUtils.getGlobalBackgroundColor());
        this.setForeground(Color.WHITE);
        this.getTableHeader().setBackground(ComponentUtils.getGlobalBackgroundColor());
        JTableHeader tableHdr = new JTableHeader();
        tableHdr.setBackground(ComponentUtils.getGlobalBackgroundColor());
        tableHdr.setForeground(ComponentUtils.getGlobalBackgroundColor());
        this.setTableHeader(tableHdr);
        this.addMouseListener(this);
        this.setFocusable(false);
        
        
        this.setShowGrid(false);
        Dimension newdim = new Dimension();
        newdim.setSize(2, 2);
        this.setIntercellSpacing(newdim);
        this.setFont(ComponentUtils.getGlobalFont());
        
        
    }
    
    
    /**
     * @param gameState
     */
    public void registerGameState(GameStateManager gameState) {
        mgr = gameState;
    }
    
    /**
     * @param gameState
     */
    public void updateGameState(GameStateManager gameState) {
        this.mgr = gameState;
    }
    
    /**
     * @param event
     */
    public void mousePressed(MouseEvent event) {
        // GNDN
    }
    
    /**
     * @param event
     */
    public void mouseReleased(MouseEvent event) {
        // GNDN
    }
    
    /**
     * @param event
     */
    public void mouseEntered(MouseEvent event) {
        // GNDN
    }
    
    /**
     * @param event
     */
    public void mouseExited(MouseEvent event) {
        // GNDN
    }
    
    /**
     * @param e
     */
    public void mouseClicked(MouseEvent e) {
        if (e.getClickCount() == 2) {
            JTable target = (JTable) e.getSource();
            int row = target.getSelectedRow();
            int col = target.getSelectedColumn();
            
            mgr.getUiManager().setDialogText(mgr.getUiManager().getDialoguePane().getText() + "\n"
                + tableHandler.getValueAt(row, col).toString() + " was clicked");
            
            // TODO open the character information screen.
        }
    }
}